package com.zlx.xBiaoShop.mapper;

import com.zlx.xBiaoShop.entity.Goods;
import com.zlx.xBiaoShop.entity.Order;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface OrderMapper {
    @Insert("INSERT INTO `xbiao_shop`.`order`(`order_id`, `recv_status`, `pay_staus`, " +
            "`user_id`, `goods_id`, `goods_count`, `total_money`,`create_date`) " +
            "VALUES (#{orderId}, #{recvStatus}, #{payStaus}, #{userId}, #{goodsId}, #{goodsCount}, #{totalMoney}, #{createDate});")
    void addOrder(Order order);

    @Select("SELECT * FROM `order` WHERE user_id = #{userId} AND goods_id = #{goodsId} AND `pay_staus` = 0 ")
    Order selectOrderByUserIdAndGoodsId(@Param("userId") Integer userId,@Param("goodsId") Integer goodsId);

    @Update("UPDATE `order` SET `goods_count` = #{goodsCount}, " +
            "`total_money` = #{totalMoney} WHERE `order_id` = #{orderId}")
    void updateTotalManeyAndCountById(@Param("goodsCount") Integer goodsCount,@Param("totalMoney") Double totalMoney,@Param("orderId") String orderId);


    @Select("SELECT * FROM `order` o WHERE o.user_id = #{userId} AND o.pay_staus =0 ORDER BY create_date DESC ")
    @Results({
            @Result(column = "order_id",property = "orderId"),
            @Result(column = "goods_id",property = "goodsId"),
            @Result(column = "goods_id",property = "goods",javaType = Goods.class,
                    one = @One(select = "com.zlx.xBiaoShop.mapper.GoodsMapper.findGoodsById",fetchType= FetchType.EAGER
                    )),
    })
    List<Order> getOderByUserId(Integer userId);

    @Update("UPDATE `order` SET `goods_count` = #{goodsCount}, `total_money` = #{totalMoney} WHERE `order_id` = #{orderId}")
    void changeGoodCount(@Param("orderId") String orderId,@Param("goodsCount") Integer goodsCount,@Param("totalMoney") Double totalMoney);


    @Delete("DELETE FROM `order` WHERE order_id = #{orderId}")
    void dedeleteOderByOrderId(String orderId);

    @Update("UPDATE `order` SET `pay_staus` = 1 WHERE `order_id` = #{orderId}")
    void updatePayStatusByOrderId(String orderId);


    @Select("SELECT * FROM `order` o WHERE o.user_id = #{userId} AND o.pay_staus =1 ORDER BY create_date DESC ")
    @Results({
            @Result(column = "order_id",property = "orderId"),
            @Result(column = "goods_id",property = "goodsId"),
            @Result(column = "goods_id",property = "goods",javaType = Goods.class,
                    one = @One(select = "com.zlx.xBiaoShop.mapper.GoodsMapper.findGoodsById",fetchType= FetchType.EAGER
                    )),
    })
    List<Order> getWaitPayOrderByUserId(Integer userId);

    @Update("UPDATE `order` SET `pay_staus` = 2 WHERE `order_id` = #{orderId}")
    void updateOrderPayStatusPayByOrderId(String orderId);

    @Select("SELECT * FROM `order` o WHERE o.user_id = #{userId} AND o.pay_staus =2 AND o.recv_status = 0 ORDER BY create_date DESC ")
    @Results({
            @Result(column = "order_id",property = "orderId"),
            @Result(column = "goods_id",property = "goodsId"),
            @Result(column = "goods_id",property = "goods",javaType = Goods.class,
                    one = @One(select = "com.zlx.xBiaoShop.mapper.GoodsMapper.findGoodsById",fetchType= FetchType.EAGER)),
    })
    List<Order> getPayedOrderByUserId(Integer userId);

    @Update("UPDATE `order` SET `recv_status` = 1 WHERE `order_id` = #{orderId} AND `pay_staus` = 2")
    void updateOrderRecvStatusByOrderId(String orderId);


    @Select("SELECT * FROM `order` o WHERE o.user_id = #{userId} AND o.pay_staus =2 AND o.recv_status = 1 ORDER BY create_date DESC ")
    @Results({
            @Result(column = "order_id",property = "orderId"),
            @Result(column = "goods_id",property = "goodsId"),
            @Result(column = "goods_id",property = "goods",javaType = Goods.class,
                    one = @One(select = "com.zlx.xBiaoShop.mapper.GoodsMapper.findGoodsById",fetchType= FetchType.EAGER
                    )),
    })
    List<Order> getRecvOrderByUserId(Integer userId);

    @Select("SELECT COUNT(1) FROM `order` WHERE pay_staus = 0 AND user_id = #{userId}")
    Integer getCarCountByUserId(Integer userId);

    @Select("SELECT COUNT(1) FROM `order` WHERE pay_staus != 0 AND user_id = #{userId}")
    Integer getOrderCountByUserId(Integer userId);
}
